package com.jiang.jetpackdemo.fragment

import android.app.ProgressDialog
import android.os.Bundle
import android.util.Log
import android.view.*
import android.widget.PopupWindow
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.jiang.jetpackdemo.R
import com.jiang.jetpackdemo.adapter.ImgAdapter
import com.jiang.jetpackdemo.ui.ImageDialog
import com.jiang.jetpackdemo.utils.dp2px
import com.jiang.jetpackdemo.utils.hasSameFile
import com.jiang.jetpackdemo.utils.saveImage
import com.jiang.jetpackdemo.utils.shareFile
import kotlinx.android.synthetic.main.pic_recommend_fragment.*

class PicRecommendFragment : Fragment() {
    companion object {
        fun newInstance() = PicRecommendFragment()
        const val TAG = "PicRecommendFragment"
        fun onT(event: MotionEvent) {
            Log.d(TAG, "onT: ${event.x}")
        }
    }

    private val viewModel: PicRecommendViewModel by lazy {
        ViewModelProvider(this).get(PicRecommendViewModel::class.java)
    }

    private val adapter: ImgAdapter by lazy {
        ImgAdapter(requireContext(), viewModel.imgList.value!!)
    }
    private val progressDialog by lazy {
        ProgressDialog(requireContext()).apply {
            setCancelable(false)
            setTitle("处理中......")
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.pic_recommend_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initView()
        viewModel.init(requireContext())
        viewModel.sizeOfimgList.observe(
            viewLifecycleOwner,
            Observer { adapter.notifyDataSetChanged() })
        viewModel.getData(false)
    }

    private fun initView() {
        rv.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        rv.adapter = adapter
        srl_pic.setOnRefreshListener {
            viewModel.getData(true, onFinish = { srl_pic.isRefreshing = false })
        }
        adapter.setLongClickListener { i, view ->
            viewModel.imgList.value?.get(i)?.let { it ->
                //  showPopWindow(view)
                ImageDialog(it, requireContext())
                    .setDownloadOnClickListener {
                        progressDialog.show()
                        viewModel.saveAImage(it.imgurl, onFinish = { progressDialog.dismiss() })
                    }
                    .setShareOnClickListener {
                        viewModel.shareImage(requireContext(), it.imgurl)
                    }
                    .setStarOnClickListener {
                        viewModel.addInFavorites(it)
                    }
                    .show()
            }
        }
    }


    private fun showPopWindow(v: View) {
        val view = LayoutInflater.from(requireContext()).inflate(R.layout.item_pic, null, false)
        val popupWindow = PopupWindow(requireContext())
        popupWindow.apply {
            width = dp2px(requireContext(), 80F)
            height = dp2px(requireContext(), 40F)
            contentView = view
            isOutsideTouchable = true
            Log.d(TAG, "showPopWindow: ${view.x} ${view.y}")
            showAsDropDown(v)
        }
    }
}


